local extension = Package("luming_idolatry")
extension.extensionName = "luming"
extension.game_modes_whitelist = {"lm_mode"}
Fk:loadTranslationTable{
  ["luming_idolatry"] = "鹿鸣-我道",
}

local U = require "packages/utility/utility"
local LM = require "packages/luming/LM"

--------------------------------------------------董卓-------------------------------------------------------------
local lm_mowang = fk.CreateTriggerSkill{
  name = "lm_mowang",
  mute = true,
  events = {fk.GameStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local svts = table.simpleClone(self.lmLordList)
    -- table.shuffle(svts)
    local max = 3
    local num = 1

    svts = table.map(svts, function(s) return LM.initServant(player, s) end)
    LM.setServantsPool(player, svts)
    LM.setServantsMax(player, max)
    local init_svts = table.random(LM.getServantsPool(player), num)
    LM.findServants(player, table.map(init_svts, Util.NameMapper), self.name)
  end
}
local lm_henzheng_tri = fk.CreateTriggerSkill{
  name = "#lm_henzheng_tri",
  mute= true,
  anim_type = "offensive",
  events = {fk.Damage},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:isWounded() and #LM.getRemoveableSvts(player) ~= 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data,"#lm_henzheng_tri_rec")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, "lm_hengzheng")
    LM.removeServantsByChc(player,1, nil, player, nil, self.name)
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name,
    })
  end,
}
local lm_hengzheng = fk.CreateActiveSkill{
  name = "lm_hengzheng",
  anim_type = "offensive",
  prompt = function(self)
    if #Self:getTableMark("hengzheng_chc-phase") == 2 then
      return ""
    elseif #Self:getTableMark("hengzheng_chc-phase") == 1 then
      return "#lm_hengzheng-" .. Self:getTableMark("hengzheng_chc-phase")[1]
    end
    return "#lm_hengzheng"
  end,
  interaction = function()
    local mark = Self:getTableMark("hengzheng_chc-phase")
    local alls = {"loseHp", "loseMaxHp"}
    local choices = {}
    for _, v in ipairs(alls) do
      if not table.contains(mark, v) then
        table.insert(choices, v)
      end
    end
    return UI.ComboBox{ choices = choices, all_choices = alls }
  end,
  can_use = function(self, player)
    return LM.canFindNewSvt(player) and #player:getTableMark("hengzheng_chc-phase") < 2
  end,
  target_filter = Util.FalseFunc,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    if not self.interaction.data then return end
    local player = room:getPlayerById(effect.from)
    local markchc = player:getTableMark("hengzheng_chc-phase")
    local choice = self.interaction.data
    if choice == "loseMaxHp" then
      room:changeMaxHp(player, -1)
    else
      room:loseHp(player, 1, self.name)
    end
    table.insert(markchc, choice)
    room:setPlayerMark(player,"hengzheng_chc-phase",markchc)
    LM.findServantsByChc(player, 1, 1, 3, self.name)
  end
}
local lm_jiuchi = fk.CreateViewAsSkill{
  name = "lm_jiuchi",
  anim_type = "offensive",
  pattern = "analeptic",
  card_num = 1,
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local c = Fk:cloneCard("analeptic")
    c:addSubcards(cards)
    c.skillName = self.name
    return c
  end,
  enabled_at_play = function(self, player)
    return Self:usedSkillTimes(self.name,Player.HistoryGame) < 2
  end,
  enabled_at_response = function(self, player, response)
    return Self:usedSkillTimes(self.name,Player.HistoryGame) < 2
  end,
}
lm_hengzheng:addRelatedSkill(lm_henzheng_tri)
local lm__lord_dongzhuo = General(extension,"lm__lord_dongzhuo","lm_k__wo",6)
-- lm__lord_dongzhuo.servant = "1/3"
-- LM.setlordServantInfo("lm__lord_dongzhuo",lm__lord_dongzhuo.servant)
lm__lord_dongzhuo:addSkill(lm_mowang)
lm__lord_dongzhuo:addSkill(lm_hengzheng)
lm__lord_dongzhuo:addSkill(lm_jiuchi)
-- lm__lord_dongzhuo:addSkill("damage_maker")
-- lm__lord_dongzhuo:addSkill("control")
-- lm__lord_dongzhuo:addSkill("rende")
-- lm__lord_dongzhuo:addSkill("cheat")
Fk:loadTranslationTable{
  ["lm__lord_dongzhuo"] = "董卓",
  ["lm_mowang"] = "魔王",
  [":lm_mowang"] = "军力技，你的初始将领上限为3。游戏开始时，你随机获得一名将领。",
  ["lm_hengzheng"] = "横征",
  [":lm_hengzheng"] = "出牌阶段各限一次，你可以：<br/>①失去1点体力；<br/>②减1点体力上限；<br/>然后发现一名将领。当你造成伤害后，你可以移除一名将领并回复1点体力。",
  ["#lm_hengzheng"] = "你可以失去1点体力或减1点体力上限，然后发现一名将领",
  ["#lm_hengzheng-loseMaxHp"] = "你可以失去1点体力，然后发现一名将领",
  ["#lm_hengzheng-loseHp"] = "你可以减1点体力上限，然后发现一名将领",
  ["#lm_henzheng_tri"] = "横征",
  ["#lm_henzheng_tri_rec"] = "横征：是否移除一名将领并回复1点体力。",
  ["lm_jiuchi"] = "酒池",
  [":lm_jiuchi"] = "每局限两次，你可以将一张牌当【酒】使用。",
  ["@[lm_servants]&lm_mowang"] = "将池",
  ["@[lm_servants]lm_mowang"] = "将领",
  ["loseMaxHp"] = "减1点体力上限",
  ["loseHp"] = "失去1点体力",
}

local lm_svt_wushuang = fk.CreateTriggerSkill{
  name = "lm_svt_wushuang",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    local names = {"slash", "duel"}
    if target and target == player and player:hasSkill(self) and data.card and table.contains(names, data.card.trueName) then
      if #player.room.logic:getEventsOfScope(GameEvent.UseCard, 2, function (e)
        return e.data[1].from == player.id and table.contains(names, e.data[1].card.trueName)
      end, Player.HistoryTurn) == 1 then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "svt_wushuang_invaild")
    room:useCard{
      card = Fk:cloneCard(data.card.name),
      from = player.id,
      tos = data.tos,
      skillName = self.name
    }
    room:removePlayerMark(player, "svt_wushuang_invaild")
  end,
}
local svt_wushuang_dist = fk.CreateDistanceSkill{
  name = "#svt_wushuang_dist",
  correct_func = function(self, from, to)
    if from:hasSkill(lm_svt_wushuang.name) then
      return -1
    end
  end,
}
local svt_wushuang_invaild = fk.CreateInvaliditySkill{
  name = "#svt_wushuang_invaild",
  invalidity_func = function(self, from, skill)
    local mark = from:getMark("svt_wushuang_invaild")
    if mark ~= 0 then
      if skill.attached_equip then
        for _, card in ipairs(Fk.cards) do
          if card.sub_type == Card.SubtypeWeapon and skill.attached_equip == card.name then
            return true
          end
        end
      end
    end
  end
}
lm_svt_wushuang:addRelatedSkill(svt_wushuang_dist)
lm_svt_wushuang:addRelatedSkill(svt_wushuang_invaild)
local lm__svt_lvbu = General(extension,"lm__svt_lvbu","lm_k__wo",0)
lm__svt_lvbu.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_lvbu")
lm__svt_lvbu:addSkill(lm_svt_wushuang)
Fk:loadTranslationTable{
  ["lm__svt_lvbu"] = "吕布",
  ["#svt_wushuang_dist"] = "无双",
  ["#svt_wushuang_invaild"] = "无双",
  ["lm_svt_wushuang"] = "无双",
  [":lm_svt_wushuang"] = "锁定技，你每回合使用第一张【杀】或【决斗】后，视为对同目标使用一张同名牌，你的武器技能失效直到此牌结算结束。你计算与其他角色的距离-1。",
}

local lm_yangwei = fk.CreateViewAsSkill{
  name = "lm_yangwei",
  anim_type = "offensive",
  pattern = "duel",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).trueName == "jink"
  end,
  view_as = function(self, cards)
    if #cards == 0 then return end
    local c = Fk:cloneCard("duel")
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  enabled_at_play = LM.phaseCanUse,
  enabled_at_response = function(self, player, response)
    return false
  end,
}
local lm_yangwei_tri = fk.CreateTriggerSkill{
  name = "#lm_yangwei_tri",
  anim_type = "offensive",
  events = {fk.TargetSpecified,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      return player:hasSkill(self) and target == player and data.card.trueName == "duel" and data.card.skillName == "lm_yangwei"
    else
      return player:hasSkill(self) and data.from and player ~= data.to and player == data.from and data.card and data.card.skillName == "lm_yangwei" and not data.to.dead and not data.from.dead
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      local to = room:getPlayerById(data.to)
      if not player.dead then
        room:drawCards(player,1,self.name)
      end
      if not to.dead then
        room:drawCards(to,1,self.name)
      end
    else
      LM.removeServantsByChc(data.to,1,nil,player, nil, self.name)
    end
  end,
}
local lm__svt_huaxiong = General(extension,"lm__svt_huaxiong","lm_k__wo",0)
lm__svt_huaxiong.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_huaxiong")
lm_yangwei:addRelatedSkill(lm_yangwei_tri)
lm__svt_huaxiong:addSkill(lm_yangwei)
Fk:loadTranslationTable{
  ["lm__svt_huaxiong"] = "华雄",
  ["#lm_yangwei_tri"] = "扬威",
  ["lm_yangwei"] = "扬威",
  [":lm_yangwei"] = "阶段技，你可以将【闪】当【决斗】使用，指定目标后与其各摸一张牌，若对其造成伤害，你移除其一名将领。",
}

local lm_baolang = fk.CreateActiveSkill{
  name = "lm_baolang",
  anim_type = "offensive",
  prompt = "#lm_baolang",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name,Player.HistoryPhase) == 0 and #LM.getRemoveableSvts(player, {"lm__svt_lijue"}) ~= 0
  end,
  interaction = function()
    return UI.ComboBox {choices = table.map(LM.getRemoveableSvts(Self, {"lm__svt_lijue"}), Util.NameMapper)}
  end,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    if not self.interaction.data then return end
    local player = room:getPlayerById(effect.from)
    local ret = LM.removeServants(player, {self.interaction.data}, player, nil, self.name)
    local damageto = room:getPlayerById(effect.tos[1])
    if ret and #ret ~= 0 and damageto then
      room:damage{
        from = player,
        to = damageto,
        damage = 1,
        skillName = self.name,
      }
      if ret[1].name == "lm__svt_guosi" then
        room:damage{
          from = player,
          to = damageto,
          damage = 1,
          skillName = self.name,
        }
      end
    end
  end
}
local lm__svt_lijue = General(extension,"lm__svt_lijue","lm_k__wo",0)
lm__svt_lijue.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_lijue")
lm__svt_lijue:addSkill(lm_baolang)
Fk:loadTranslationTable{
  ["lm__svt_lijue"] = "李傕",
  ["lm_baolang"] = "暴狼",
  [":lm_baolang"] = "阶段技，你可以移除一名其他将领并对一名角色造成1点伤害，若移除郭汜则额外造成1点伤害。",
  ["#lm_baolang"] = "你可以移除一名其他将领并对一名角色造成1点伤害，若移除郭汜则额外造成1点伤害。",
}

local lm_tanbei = fk.CreateActiveSkill{
  name = "lm_tanbei",
  anim_type = "offensive",
  prompt = "#lm_tanbei",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name,Player.HistoryPhase) == 0 and #LM.getRemoveableSvts(player, {"lm__svt_guosi"}) ~= 0
  end,
  interaction = function()
    return UI.ComboBox {choices = table.map(LM.getRemoveableSvts(Self, {"lm__svt_guosi"}), Util.NameMapper)}
  end,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    if not self.interaction.data then return end
    local player = room:getPlayerById(effect.from)
    local ret = LM.removeServants(player, {self.interaction.data}, player, nil, self.name)
    local getcardto = room:getPlayerById(effect.tos[1])
    if ret then
      room:drawCards(player,1,self.name)
      if getcardto then
        local getcid = room:askForCardChosen(player,getcardto,"he", self.name)
        room:obtainCard(player.id,getcid,true,fk.ReasonJustMove)
        if ret[1] == "lm__svt_lijue" and not getcardto:isNude() then
          getcid = room:askForCardChosen(player,getcardto,"he", self.name)
          room:obtainCard(player.id,getcid,true,fk.ReasonJustMove)
        end
      end
    end
  end
}
local lm__svt_guosi = General(extension,"lm__svt_guosi","lm_k__wo",0)
lm__svt_guosi.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_guosi")
lm__svt_guosi:addSkill(lm_tanbei)
Fk:loadTranslationTable{
  ["lm__svt_guosi"] = "郭汜",
  ["lm_tanbei"] = "贪狈",
  [":lm_tanbei"] = "阶段技，你可以移除一名其他将领，然后摸一张牌并获得一名其他角色一张牌，若移除李傕则额外获得其一张牌。",
  ["#lm_tanbei"] = "你可以移除一名其他将领，然后摸一张牌并获得一名其他角色一张牌，若移除李傕则额外获得其一张牌",
}

local lm_xianzhen = fk.CreateTriggerSkill{
  name = "lm_xianzhen",
  mute = true,
  events = {fk.TargetSpecified, LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or target ~= player then
      return
    end
    if event == fk.TargetSpecified then
      return player:hasSkill(self) and target == player and data.card and data.card.trueName == "slash"
    else
      return table.find(data.svts, function(svt) return svt.name == "lm__svt_gaoshun" end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      return true
    else
      local others = {}
      for _,pr in ipairs(room.alive_players) do
        if player~=pr and not pr:getEquipment(Card.SubtypeArmor) then
          table.insert(others,pr.id)
        end
      end
      if #others == 0 then return end
      local tos = room:askForChoosePlayers(player,others,1,1,"#lm_xianzhen_damage",self.name,true)
      if #tos > 0 then
        self.cost_data = tos[1]
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      room:addPlayerMark(room:getPlayerById(data.to), fk.MarkArmorNullified)
      data.extra_data = data.extra_data or {}
      data.extra_data.xianzhen = data.extra_data.xianzhen or {}
      data.extra_data.xianzhen[tostring(data.to)] = (data.extra_data.xianzhen[tostring(data.to)] or 0) + 1
    else
      room:notifySkillInvoked(player, self.name, "offensive", {self.cost_data})
      room:damage{
        from = player,
        to = room:getPlayerById(self.cost_data),
        damage = 1,
        skillName = self.name,
      }
    end
  end,
  refresh_events = {fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.xianzhen
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for key, num in pairs(data.extra_data.xianzhen) do
      local p = room:getPlayerById(tonumber(key))
      if p:getMark(fk.MarkArmorNullified) > 0 then
        room:removePlayerMark(p, fk.MarkArmorNullified, num)
      end
    end
    data.extra_data.xianzhen = nil
  end,
}

local lm__svt_gaoshun = General(extension,"lm__svt_gaoshun","lm_k__wo",0)
lm__svt_gaoshun.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_gaoshun")
lm__svt_gaoshun:addSkill(lm_xianzhen)
Fk:loadTranslationTable{
  ["lm__svt_gaoshun"] = "高顺",
  ["#lm_xianzhen_damage"] = "陷阵：你可以对一名未装备防具的角色造成1点伤害。",
  ["lm_xianzhen"] = "陷阵",
  [":lm_xianzhen"] = "你使用【杀】无视防具。你移除此将领后，可以对一名未装备防具的角色造成1点伤害。",
}

local lm_dz_tuxi = fk.CreateTriggerSkill{
  name = "lm_dz_tuxi",
  anim_type = "offensive",
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target and #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
      local use = e.data[1]
      return use.from == player.id and use.card.trueName == "slash"
    end, Player.HistoryTurn) == 0 and table.find(player.player_cards[Player.Hand],function(cid) return Fk:getCardById(cid).trueName == "slash" end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local others = {}
    for _, pr in ipairs(room.alive_players) do
      if player ~= pr and not pr:isNude() then
        table.insert(others, pr.id)
      end
    end
    if #others == 0 then return end
    local targets, showcids = room:askForChooseCardAndPlayers(player, others, 1, 1, "slash|.|.|.", "#lm_dz_tuxi_show", self.name, true)
    if #targets > 0 then
      self.cost_data = {targets[1], showcids}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:showCards(self.cost_data[2])
    local cid = room:askForCardChosen(player, room:getPlayerById(self.cost_data[1]), "he", self.name)
    room:obtainCard(player.id,cid,true,fk.ReasonJustMove)
  end,
}
local lm__svt_zhangliao = General(extension,"lm__svt_zhangliao","lm_k__wo",0)
lm__svt_zhangliao.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_zhangliao")
lm__svt_zhangliao:addSkill(lm_dz_tuxi)
Fk:loadTranslationTable{
  ["lm__svt_zhangliao"] = "张辽",
  ["#lm_dz_tuxi_show"] = "突袭：你可以展示一张【杀】并获得一名其他角色一张牌。",
  ["lm_dz_tuxi"] = "突袭",
  [":lm_dz_tuxi"] = "回合结束时，若你本回合未使用过【杀】，你可以展示一张【杀】并获得一名其他角色一张牌。",
}

local lm_dz_fuji = fk.CreateTriggerSkill{
  name = "lm_dz_fuji",
  anim_type = "offensive",
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player ~= target and player:inMyAttackRange(target) and player.hp ~= target.hp
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    self.cost_data = nil
    if player.hp < target.hp then
      return room:askForSkillInvoke(player, self.name, data,"#lm_dz_fuji_draw")
    end
    if player.hp > target.hp then
      local usecard = room:askForUseCard(player, "slash", "slash", "#lm_dz_fuji_slash", true, {must_targets = {target.id}, bypass_distances = true})
      if usecard then
        self.cost_data = usecard
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data then
      room:useCard(self.cost_data)
    else
      room:drawCards(player,1,self.name)
    end
  end,
}
local lm__svt_xurong = General(extension,"lm__svt_xurong","lm_k__wo",0)
lm__svt_xurong.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_xurong")
lm__svt_xurong:addSkill(lm_dz_fuji)
Fk:loadTranslationTable{
  ["lm__svt_xurong"] = "徐荣",
  ["#lm_dz_fuji_draw"] = "伏击：你可以摸一张牌。",
  ["#lm_dz_fuji_slash"] = "伏击：你可以对其使用一张【杀】",
  ["lm_dz_fuji"] = "伏击",
  [":lm_dz_fuji"] = "你攻击范围内的角色回合结束时，若其体力值：少于你，你可以对其使用一张【杀】；多于你，你可以摸一张牌。",
}

local lm_dz_fencheng = fk.CreateActiveSkill{
  name = "lm_dz_fencheng",
  anim_type = "offensive",
  prompt = "#lm_dz_fencheng",
  can_use = function(self, player)
    return true
  end,
  target_filter = Util.FalseFunc,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    LM.removeServants(player,{"lm__svt_liru"}, player, nil, self.name)
    for _,pr in ipairs(room.alive_players) do
      if player~=pr then
        local equips = pr.player_cards[Player.Equip]
        if #equips == 0 then
          room:damage{
            from = player,
            to = pr,
            damage = 1,
            damageType = fk.FireDamage,
            skillName = self.name,
          }
        else
          room:throwCard(table.clone(equips), self.name, pr, player)
        end
      end
    end
  end
}
local lm__svt_liru = General(extension,"lm__svt_liru","lm_k__wo",0)
lm__svt_liru.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_liru")
lm__svt_liru:addSkill(lm_dz_fencheng)
Fk:loadTranslationTable{
  ["lm__svt_liru"] = "李儒",
  ["lm_dz_fencheng"] = "焚城",
  [":lm_dz_fencheng"] = "出牌阶段，你可以移除此将领，令所有其他角色依次弃置装备区的所有牌，装备区没牌则改为受到1点火焰伤害。",
  ["#lm_dz_fencheng"] = "你可以移除李儒，令所有其他角色依次弃置装备区的所有牌，装备区没牌则改为受到1点火焰伤害",
}

local lm_dz_tunjun = fk.CreateActiveSkill{
  name = "lm_dz_tunjun",
  anim_type = "offensive",
  prompt = "#lm_dz_tunjun",
  can_use = function(self, player)
    return player:getHandcardNum() < 5
  end,
  target_filter = Util.FalseFunc,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    LM.removeServants(player,{"lm__svt_niufu"}, player, nil, self.name)
    room:setPlayerMark(player,"@@tunjun_proed-phase",1)
    local hands = player.player_cards[Player.Hand]
    if #hands < 5 then
      room:drawCards(player,5-#hands,self.name)
    end
  end
}
local lm_dz_tunjun_prohi = fk.CreateProhibitSkill{
  name = "#lm_dz_tunjun_prohi",
  is_prohibited = function(self, from, to, card)
    return from:getMark("@@tunjun_proed-phase") ~= 0 and from ~= to
  end,
}
lm_dz_tunjun:addRelatedSkill(lm_dz_tunjun_prohi)
local lm__svt_niufu = General(extension,"lm__svt_niufu","lm_k__wo",0)
lm__svt_niufu.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_niufu")
lm__svt_niufu:addSkill(lm_dz_tunjun)
Fk:loadTranslationTable{
  ["lm__svt_niufu"] = "牛辅",
  ["lm_dz_tunjun"] = "屯军",
  [":lm_dz_tunjun"] = "出牌阶段，你可以移除此将领并将手牌补至五张，本阶段你不能对其他角色使用牌。",
  ["#lm_dz_tunjun"] = "你可以移除牛辅并将手牌补至五张，本阶段你不能对其他角色使用牌",
  ["@@tunjun_proed-phase"] = "屯军",
}

local lm_dz_lihui = fk.CreateActiveSkill{
  name = "lm_dz_lihui",
  anim_type = "offensive",
  prompt = "#lm_dz_lihui",
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return true
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id and LM.getLordSkill(Fk:currentRoom():getPlayerById(to_select))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:obtainCard(target.id,effect.cards,true,fk.ReasonGive)
    LM.changeSevtToTarget(player,target,"lm__svt_lisu", true, self.name)
    if LM.canFindNewSvt(player) then
      local remove = LM.removeServantsByChc(target,1,{"lm__svt_lisu"}, player, true, self.name)
      if #remove ~= 0 then
        LM.findServants(player, remove, self.name)
      end
    end
  end
}
local lm__svt_lisu = General(extension,"lm__svt_lisu","lm_k__wo",0)
lm__svt_lisu.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_lisu")
lm__svt_lisu:addSkill(lm_dz_lihui)
Fk:loadTranslationTable{
  ["lm__svt_lisu"] = "李肃",
  ["lm_dz_lihui"] = "利贿",
  [":lm_dz_lihui"] = "出牌阶段，你可以将此将领洗入一名角色的将领池并交给其一张装备牌，然后将其一名其他将领置入你的空将领位。",
  ["#lm_dz_lihui"] = "你可以将此将领洗入一名角色的将领池并交给其一张装备牌，然后将其一名其他将领置入你的空将领位",
}

local lm_dz_luanwu = fk.CreateTriggerSkill{
  name = "lm_dz_luanwu",
  anim_type = "offensive",
  events = {LM.importedServants, LM.findServant},
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == LM.importedServants then
        return table.find(data.svts, function(svt) return svt.name == "lm__svt_jiaxu" end)
      else
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == LM.importedServants then
      return true
    else
      local suit = {}
      if LM.canFindServant(player, "lm__svt_lijue") then
        table.insert(suit, "heart")
      end
      if LM.canFindServant(player, "lm__svt_guosi") then
        table.insert(suit, "spade")
      end
      if #suit == 0 then return end
      local prompt = #suit == 2 and "#lm_dz_luanwu_all" or ("#lm_dz_luanwu_" .. suit[1])
      local discard = player.room:askForDiscard(player, 1, 1, true, self.name, true, ".|.|" .. table.concat(suit, ","), prompt, true)
      if #discard ~= 0 then
        self.cost_data = discard
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == LM.importedServants then
      room:drawCards(player,2,self.name)
    else
      room:throwCard(self.cost_data, self.name, player, player)
      local card = Fk:getCardById(self.cost_data[1])
      local svt = LM.getServant(player, card.suit == Card.Heart and "lm__svt_lijue" or "lm__svt_guosi", true)
      if svt then
        LM.setServantsPool(player, data.svts)
        data.svts = {}
        LM.removeServantFromPool(player, svt.name)
        data.result = {svt}
      end
    end
  end
}
local lm__svt_jiaxu = General(extension,"lm__svt_jiaxu","lm_k__wo",0)
lm__svt_jiaxu.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_jiaxu")
lm__svt_jiaxu:addSkill(lm_dz_luanwu)
Fk:loadTranslationTable{
  ["lm__svt_jiaxu"] = "贾诩",
  ["lm_dz_luanwu"] = "乱武",
  [":lm_dz_luanwu"] = "你获得此将领后，摸两张牌。你发现将领时，可以弃置一张红桃牌/黑桃牌，改为获得李傕/郭汜。",
  ["#lm_dz_luanwu_all"] = "乱武：你可以弃置一张红桃牌并改为获得李傕，或弃置一张黑桃牌并改为获得郭汜",
  ["#lm_dz_luanwu_heart"] = "乱武：你可以弃置一张红桃牌并改为获得李傕",
  ["#lm_dz_luanwu_spade"] = "乱武：你可以弃置一张黑桃牌并改为获得郭汜",
}

local lm_dz_xiahui = fk.CreateViewAsSkill{
  name = "lm_dz_xiahui",
  anim_type = "offensive",
  pattern = "ex_nihilo,slash",
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local cardname = Fk:getCardById(cards[1]).suit == Card.Spade and "ex_nihilo" or "slash"
    local c = Fk:cloneCard(cardname)
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  card_filter = function(self, to_select, selected)
    if #selected ~= 0 then return end
    local card = Fk:getCardById(to_select)
    local mark = Self:getTableMark("lm_dz_xiahui-turn")
    return table.contains({Card.Club, Card.Spade}, card.suit) and not table.contains(mark, card.suit)
  end,
  before_use = function(self, player, use)
    if not use.card or not table.contains({Card.Heart, Card.Spade}, use.card.suit) then return end
    local mark = player:getTableMark("lm_dz_xiahui-turn")
    table.insert(mark, use.card.suit)
    player.room:setPlayerMark(player, "lm_dz_xiahui-turn", mark)
  end,
  enabled_at_play = function(self, player)
    local mark = player:getTableMark("lm_dz_xiahui-turn")
    return #mark < 2
  end,
  enabled_at_response = function(self, player, response)
    local mark = player:getTableMark("lm_dz_xiahui-turn")
    return not response and #mark < 2
  end,
}
local lm_dz_jiwei = fk.CreateActiveSkill{
  name = "lm_dz_jiwei",
  anim_type = "offensive",
  prompt = "#lm_dz_jiwei",
  can_use = function(self, player)
    return true
  end,
  target_filter = Util.FalseFunc,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local changename = "lm__svt_dongbai"
    LM.removeServants(player, {changename}, player, true, self.name)
    LM.changeLord(player,changename, false, true, false)
    local skills = table.map(Fk.generals[changename]:getSkillNameList(), function(e)
      return "-" .. e
    end)
    room:handleAddLoseSkills(player, table.concat(skills, "|"), nil)

    local mhp = 3 - player.maxHp
    if mhp ~= 0 then room:changeMaxHp(player, mhp) end
    local hp = 3 - player.hp
    if hp ~= 0 then room:changeHp(player, hp) end
    local num = 3 - #player.player_cards[Player.Hand]
    if num > 0 then
      local topCards = room:getNCards(num)
      room:moveCards({
        ids = topCards,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = self.name,
      })
    elseif num < 0 then
      num = -num
      local discard = room:askForCard(player,num,num,false,self.name,false, nil, "#lm_dz_jiwei-discard:::" .. num)
      if discard then
        room:moveCards({
          ids = discard,
          from = player.id,
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonPutIntoDiscardPile,
          proposer = player.id,
          skillName = self.name,
        })
      end
    end
    room:handleAddLoseSkills(player, "lm_zhenzhao", nil)
  end
}
local lm__svt_dongbai = General(extension,"lm__svt_dongbai","lm_k__wo",0)
lm__svt_dongbai.gender = General.Female
lm__svt_dongbai.hidden = true
LM.setlordServant(lm_mowang,"lm__svt_dongbai")
lm__svt_dongbai:addSkill(lm_dz_xiahui)
lm__svt_dongbai:addSkill(lm_dz_jiwei)
Fk:loadTranslationTable{
  ["lm__svt_dongbai"] = "董白",
  ["@[private]&lm__svt_dongbai_svts"] = "将池",
  ["@[private]&lm__svt_dongbai_getsvts"] = "将领",
  ["lm_dz_xiahui"] = "黠慧",
  [":lm_dz_xiahui"] = "每回合各限一次，你可以将黑桃牌/梅花牌当【无中生有】/【杀】使用。",
  ["lm_dz_jiwei"] = "继位",
  [":lm_dz_jiwei"] = "出牌阶段，你可以用此牌替换董卓当主公角色，将体力、体力上限、手牌数皆调整为3，获得技能〖征召〗。",
  ["#lm_dz_jiwei"] = "你可以用董白替换董卓当主公角色，将体力、体力上限、手牌数皆调整为3，获得技能〖征召〗",
  ["#lm_dz_jiwei-discard"] = "继位：请将%arg张手牌置入弃牌堆",
}

return extension